function [export_revenues_US,export_revenues_OECD,export_share_US,export_share_OECD,wage_level_US,wage_level_OECD,...
    real_consumption_US,real_consumption_OECD,output_US,output_OECD,prices_US,prices_OECD,exports_US,exports_OECD,...
    imports_elasticity_measure_US,imports_elasticity_measure_OECD] ...
    = find_equilibrium_trade_elasticity_real(wage_guess_OECD,qualities_vec_US,qualities_vec_OECD,domestic_competitor_qual_vec_US,domestic_competitor_qual_vec_OECD,...
    import_tariff_US,import_tariff_OECD,L_US,L_OECD,n_varieties,export_share_US_guess,export_share_OECD_guess,mu_bar_US_guess,mu_bar_OECD_guess,sigg)

wage_US=1;
wage.guess_OECD = wage_guess_OECD;
wage_distance = 100;
mu_bar.step7_US = mu_bar_US_guess;
mu_bar.step7_OECD = mu_bar_OECD_guess;
export_share.step6_US = export_share_US_guess;
export_share.step6_OECD = export_share_OECD_guess;
counter_outer=0;
step7_distance=100;
mu_US_distance=100;
mu_OECD_distance=100;
mu_OECD_bounce_list=[];
mu_US_bounce_list=[];

% ITERATE STEPS 1-3 TO FIND WAGE THAT SATISFIES BALANCED TRADE
while (abs(step7_distance) > 1e-2 || abs(mu_US_distance)>1e-2 || abs(mu_OECD_distance)>1e-2) && counter_outer<=100
    counter_outer=counter_outer+1;
    counter=0;
    wage_bounce_list=[];

find_equilibrium_innerloop_sigma

% START WITH STEP 4 - GUESS MU_BAR USING EXPORT SHARE DATA
    
    mu_bar.step4_US = 1/((1 - export_share.step6_US) * (sum((1./markups_US(produced_US)) .* ...
                                                                      ((1 ./ ((prices_US(produced_US) ./ qualities_vec_US(produced_US)) .^ (sigg - 1))) ...
                                                                      ./ (sum(1./((prices_US(produced_US) ./ qualities_vec_US(produced_US)) .^ (sigg - 1))))))) ...
                                                                      + export_share.step6_US * (sum((1./markups_OECD(exports_US)) ...
                                                                      .* ((1 ./ ((prices_OECD(exports_US) ./ qualities_vec_US(exports_US)) .^ (sigg - 1))) ...
                                                                      ./ sum(1./((prices_OECD(exports_US) ./ qualities_vec_US(exports_US)) .^ (sigg - 1)))))));
                                                                 
                                                                  
    mu_bar.step4_OECD = 1/((1 - export_share.step6_OECD) * (sum((1./markups_OECD(produced_OECD)) .* ...
                                                                      ((1 ./ ((prices_OECD(produced_OECD) ./ qualities_vec_OECD(produced_OECD)) .^ (sigg - 1))) ...
                                                                      ./ (sum(1./((prices_OECD(produced_OECD) ./ qualities_vec_OECD(produced_OECD)) .^ (sigg - 1))))))) ...
                                                                      + export_share.step6_OECD * (sum((1./markups_US(exports_OECD)) ...
                                                                      .* ((1 ./ ((prices_US(exports_OECD) ./ qualities_vec_OECD(exports_OECD)) .^ (sigg - 1))) ...
                                                                      ./ sum(1./((prices_US(exports_OECD) ./ qualities_vec_OECD(exports_OECD)) .^ (sigg - 1)))))));       
    
    %Step 5 -- calculate output
     output_US = nan(size(prices_US));
    output_US(produced_US) = ((wage_US * L_US * mu_bar.step4_US + (import_tariff_US-1) * export_share.step6_OECD * wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD) ...
        / P_bar_US).*(P_bar_US ./ prices_US(produced_US)).^(sigg); 
    output_US(exports_OECD) = ((wage_US * L_US * mu_bar.step4_US + (import_tariff_US-1) * export_share.step6_OECD * wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD) ...
        / P_bar_US).*(P_bar_US ./ prices_US(exports_OECD)).^(sigg);
    
    output_OECD = nan(size(prices_US));
    output_OECD(produced_OECD) = ((wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD + (import_tariff_OECD-1) * export_share.step6_US * wage_US * L_US * mu_bar.step4_US) ...
        / P_bar_OECD) .* ( P_bar_OECD ./ prices_OECD(produced_OECD)).^(sigg);
    output_OECD(exports_US) = ((wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD + (import_tariff_OECD-1) * export_share.step6_US * wage_US * L_US * mu_bar.step4_US) ...
        / P_bar_OECD) .* ( P_bar_OECD ./ prices_OECD(exports_US)).^(sigg);
    
    %Step 6 
    GDP_US = sum(output_US(produced_US).*prices_US(produced_US)) + sum(output_OECD(exports_US).*prices_OECD(exports_US))/import_tariff_OECD;
    GDP_OECD = sum(output_OECD(produced_OECD).*prices_OECD(produced_OECD)) + sum(output_US(exports_OECD).*prices_US(exports_OECD))/import_tariff_US;
    
    %export share step6
    export_share.step6_US = (sum(output_OECD(exports_US).*prices_OECD(exports_US))/import_tariff_OECD)/GDP_US;
    export_share.step6_OECD = (sum(output_US(exports_OECD).*prices_US(exports_OECD))/import_tariff_US)/GDP_OECD;
    
    
    %Step 7 
    mu_bar.step4_US = 1/((1 - export_share.step6_US) * (sum((1./markups_US(produced_US)) .* ...
                                                                      ((1 ./ ((prices_US(produced_US) ./ qualities_vec_US(produced_US)) .^ (sigg - 1))) ...
                                                                      ./ (sum(1./((prices_US(produced_US) ./ qualities_vec_US(produced_US)) .^ (sigg - 1))))))) ...
                                                                      + export_share.step6_US * (sum((1./markups_OECD(exports_US)) ...
                                                                      .* ((1 ./ ((prices_OECD(exports_US) ./ qualities_vec_US(exports_US)) .^ (sigg - 1))) ...
                                                                      ./ sum(1./((prices_OECD(exports_US) ./ qualities_vec_US(exports_US)) .^ (sigg - 1)))))));
    mu_bar.step4_OECD = 1/((1 - export_share.step6_OECD) * (sum((1./markups_OECD(produced_OECD)) .* ...
                                                                      ((1 ./ ((prices_OECD(produced_OECD) ./ qualities_vec_OECD(produced_OECD)) .^ (sigg - 1))) ...
                                                                      ./ (sum(1./((prices_OECD(produced_OECD) ./ qualities_vec_OECD(produced_OECD)) .^ (sigg - 1))))))) ...
                                                                      + export_share.step6_OECD * (sum((1./markups_US(exports_OECD)) ...
                                                                      .* ((1 ./ ((prices_US(exports_OECD) ./ qualities_vec_OECD(exports_OECD)) .^ (sigg - 1))) ...
                                                                      ./ sum(1./((prices_US(exports_OECD) ./ qualities_vec_OECD(exports_OECD)) .^ (sigg - 1)))))));       
                                                                  
                                                                  
    % NEED TO FIX THIS AS WELL TO REFLECT THE NEW BALANCED TRADE CONDITION                                                             
    %wage.step7_OECD = (mu_bar.step7_OECD*wage_US*L_US*m_guess)/(mu_bar.step7_US*L_OECD*x_guess);
    wage.step7_OECD = abs(((mu_bar.step7_US*wage_US*L_US*((P_bar_US^(sigg-1)).*sum(1./(prices_US(exports_OECD)./qualities_vec_OECD(exports_OECD))))*import_tariff_OECD) ...
                                ./(((P_bar_OECD^(sigg-1)).*sum(1./(prices_OECD(exports_US)./qualities_vec_US(exports_US))))*import_tariff_US)-...
                                (import_tariff_OECD-1) * export_share.step6_US * mu_bar.step7_US * wage_US * L_US)...
                                /(mu_bar.step7_OECD * L_OECD - (import_tariff_US-1) * (((P_bar_US^(sigg-1)).*sum(1./(prices_US(exports_OECD)./qualities_vec_OECD(exports_OECD)))) * import_tariff_OECD) ...
                                / (((P_bar_OECD^(sigg-1)).*sum(1./(prices_OECD(exports_US)./qualities_vec_US(exports_US)))) * import_tariff_US) * export_share.step6_OECD * mu_bar.step7_OECD * L_OECD));
    
    % DISTANCE BETWEEN BALANCED TRADE WAGE AND MU_BAR GUESSES WAGE
    step7_distance = wage.step7_OECD - wage.balanced_trade_OECD;
    if counter_outer<5
         wage.guess_OECD = wage.balanced_trade_OECD + 0.05*step7_distance;
         mu_OECD_bounce_list = [mu_OECD_bounce_list; mu_bar.step4_OECD; mu_bar.step7_OECD];
         mu_US_bounce_list = [mu_US_bounce_list; mu_bar.step4_US; mu_bar.step7_US];
    else
         wage.guess_OECD = wage.balanced_trade_OECD + 0.01*step7_distance;
         mu_OECD_bounce_list = [mu_OECD_bounce_list; mu_bar.step4_OECD; mu_bar.step7_OECD];
         mu_US_bounce_list = [mu_US_bounce_list; mu_bar.step4_US; mu_bar.step7_US];
    end

    mu_US_distance = mu_bar.step7_US - mu_bar.step4_US;
    if counter_outer <= 5
         mu_bar.step7_US = 0.5*mu_bar.step4_US+0.5*mu_bar.step7_US;       
    elseif counter_outer>5 && counter_outer <= 10
         mu_bar.step7_US = 0.1*mu_bar.step4_US+0.9*mu_bar.step7_US;
    elseif counter_outer > 10
         mu_bar.step7_US = mean(mu_US_bounce_list(end-3:end));
    end

%     if counter < 6
%          mu_bar.step7_US = 0.5*mu_bar.step4_US+0.5*mu_bar.step7_US;       
%     else
%          mu_bar.step7_US = 0.1*mu_bar.step4_US+0.9*mu_bar.step7_US;
%     end
    
    mu_OECD_distance = mu_bar.step7_OECD - mu_bar.step4_OECD;
    if counter_outer <= 5
         mu_bar.step7_OECD = 0.5*mu_bar.step4_OECD+0.5*mu_bar.step7_OECD;
    elseif counter_outer>5 && counter_outer <= 10
         mu_bar.step7_OECD = 0.1*mu_bar.step4_OECD+0.9*mu_bar.step7_OECD;
    elseif counter_outer > 10
         mu_bar.step7_OECD = mean(mu_OECD_bounce_list(end-3:end));
    end

%     if counter < 6
%          mu_bar.step7_OECD = 0.5*mu_bar.step4_OECD+0.5*mu_bar.step7_OECD;       
%     else
%          mu_bar.step7_OECD = 0.1*mu_bar.step4_OECD+0.9*mu_bar.step7_OECD;
%     end

    wage_distance = step7_distance;

%     if counter>=990
%         disp('ugh')
%     end

end

    % IF CONVERGENCE DOES NOT OCCUR IN 150 LOOPS USE THE AVERAGES
 
% TAKE ALL THE FINAL VARIABLE VALUES AND NAME THEM OUTPUT FROM THIS FUNCTION
if counter_outer>=100;
    %disp('DID NOT CONVERGE')
    mu_bar_US = mean(mu_US_bounce_list(end-99:end));
    mu_bar_OECD = mean(mu_OECD_bounce_list(end-99:end));
    find_equilibrium_innerloop_sigma
    wage_OECD = wage.balanced_trade_OECD;
    
    %Calculate output using averaged mu's from bounce list
    output_US = nan(size(prices_US));
    output_US(produced_US) = ((wage_US * L_US * mu_bar.step4_US + (import_tariff_US-1) * export_share.step6_OECD * wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD) ...
        / P_bar_US).*(P_bar_US ./ prices_US(produced_US)).^(sigg); 
    output_US(exports_OECD) = ((wage_US * L_US * mu_bar.step4_US + (import_tariff_US-1) * export_share.step6_OECD * wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD) ...
        / P_bar_US).*(P_bar_US ./ prices_US(exports_OECD)).^(sigg);
    
    output_OECD = nan(size(prices_US));
    output_OECD(produced_OECD) = ((wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD + (import_tariff_OECD-1) * export_share.step6_US * wage_US * L_US * mu_bar.step4_US) ...
        / P_bar_OECD) .* ( P_bar_OECD ./ prices_OECD(produced_OECD)).^(sigg);
    output_OECD(exports_US) = ((wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD + (import_tariff_OECD-1) * export_share.step6_US * wage_US * L_US * mu_bar.step4_US) ...
        / P_bar_OECD) .* ( P_bar_OECD ./ prices_OECD(exports_US)).^(sigg);
    
    %Step 6 
    GDP_US = sum(output_US(produced_US).*prices_US(produced_US)) + sum(output_OECD(exports_US).*prices_OECD(exports_US))/import_tariff_OECD;
    GDP_OECD = sum(output_OECD(produced_OECD).*prices_OECD(produced_OECD)) + sum(output_US(exports_OECD).*prices_US(exports_OECD))/import_tariff_US;
    
    %export share step6
    export_share_US = (sum(output_OECD(exports_US).*prices_OECD(exports_US))/import_tariff_OECD)/GDP_US;
    export_share_OECD = (sum(output_US(exports_OECD).*prices_US(exports_OECD))/import_tariff_US)/GDP_OECD;
    
else
    
wage_OECD = wage.balanced_trade_OECD;
export_share_US=export_share.step6_US;
export_share_OECD=export_share.step6_OECD;
mu_bar_US = mu_bar.step7_US;
mu_bar_OECD = mu_bar.step7_OECD;
end

revenue_US = prices_US.*output_US;
revenue_OECD = prices_OECD.*output_OECD;

x = sum(exports_US) / length(exports_US);
m = sum(exports_OECD) / length(exports_OECD);

qualities_vec_consumed_US=qualities_vec_US;
qualities_vec_consumed_US(exports_OECD)=qualities_vec_OECD(exports_OECD);
    
qualities_vec_consumed_OECD=qualities_vec_OECD;
qualities_vec_consumed_OECD(exports_US)=qualities_vec_US(exports_US);

wage_level_US = geomean(abs(real([qualities_vec_US(produced_US)./markups_US(produced_US); ...
    (qualities_vec_OECD(exports_OECD)./markups_US(exports_OECD))*(wage_US/(wage_OECD*import_tariff_US))])));

wage_level_OECD = geomean(abs(real([qualities_vec_OECD(produced_OECD)./markups_OECD(produced_OECD); ...
    (qualities_vec_US(exports_US)./markups_OECD(exports_US))*(wage_OECD/(wage_US*import_tariff_OECD))])));

real_product_wage_US = prod((qualities_vec_US(produced_US)./markups_US(produced_US)).^(revenue_US(produced_US)./GDP_US)) ...
    * prod((qualities_vec_US(exports_US)./(markups_OECD(exports_US)*import_tariff_OECD)).^(revenue_OECD(exports_US)./(import_tariff_OECD*GDP_US)));

real_product_wage_OECD = prod((qualities_vec_OECD(produced_OECD)./markups_OECD(produced_OECD)).^(revenue_OECD(produced_OECD)./GDP_OECD)) ...
    * prod((qualities_vec_OECD(exports_OECD)./(markups_US(exports_OECD)*import_tariff_US)).^(revenue_US(exports_OECD)./(import_tariff_US*GDP_OECD)));

P_bar_US = (sum((prices_US(produced_US) ./ qualities_vec_US(produced_US)).^(1-sigg)) + sum((prices_US(exports_OECD) ./ qualities_vec_OECD(exports_OECD)).^(1-sigg)))^(1/(1-sigg));
P_bar_OECD = (sum((prices_OECD(produced_OECD) ./ qualities_vec_OECD(produced_OECD)).^(1-sigg)) + sum((prices_OECD(exports_US) ./ qualities_vec_US(exports_US)).^(1-sigg)))^(1/(1-sigg));
   
laborshare_US = 1/mu_bar_US;
laborshare_OECD = 1/mu_bar_OECD;

total_income_US = wage_US * L_US * mu_bar_US + (import_tariff_US-1) * export_share_OECD * wage_OECD * L_OECD * mu_bar_OECD;
total_income_OECD = wage_OECD * L_OECD * mu_bar_OECD + (import_tariff_OECD-1) * export_share_US * wage_US * L_US * mu_bar_US;

real_consumption_US = total_income_US/P_bar_US;
real_consumption_OECD = total_income_OECD/P_bar_OECD;

export_revenues_US = (sum(output_OECD(exports_US).*prices_OECD(exports_US))/import_tariff_OECD);
export_revenues_OECD = (sum(output_US(exports_OECD).*prices_US(exports_OECD))/import_tariff_US);

imports_elasticity_measure_US = (sum(output_US(exports_OECD).*prices_US(exports_OECD)))./ ...
    sum(output_US(produced_US).*prices_US(produced_US));

imports_elasticity_measure_OECD = (sum(output_OECD(exports_US).*prices_OECD(exports_US)))./ ...
    sum(output_OECD(produced_OECD).*prices_OECD(produced_OECD));

end


